package com.hongye.manager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongye.common.entity.InformationEntity;
import com.hongye.common.response.ResponseResult;
import com.hongye.manager.mapper.InformationMapper;
import com.hongye.manager.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/information")
public class InformationController {

    @Autowired
    private InformationService service;

    @Autowired
    private InformationMapper informationMapper;

    @RequestMapping("/findPage")
    public List<InformationEntity> fenye(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "size",defaultValue = "10") int size, @RequestParam String name){
        //创建分页对象
        Page<InformationEntity> paged = new Page<>(page,size);
        List<InformationEntity> jobInfos = service.selectUserPage(paged,name);
        return jobInfos;
    }

    @RequestMapping("/del")
    public ResponseResult del(Integer id){
        int i = informationMapper.deleteById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("i",i);
        return ResponseResult.ok(1,"成功",map);
    }

    @RequestMapping("/add")
    public Object add(@RequestBody InformationEntity informationEntity){
        System.out.println(informationEntity);
        if (service.isExist(informationEntity)){
            return ResponseResult.error(1,"用户名重复,请重新输入");
        }
        boolean save = service.save(informationEntity);

        return save;
    }

    @RequestMapping("/update")
    public ResponseResult update(@RequestBody InformationEntity informationEntity){

        InformationEntity information = new InformationEntity();

        information.setId(informationEntity.getId());
        information.setInformationTitle(informationEntity.getInformationTitle());
        information.setLastTime(informationEntity.getLastTime());
        information.setResource(informationEntity.getResource());
        information.setReleaseTime(informationEntity.getReleaseTime());
        information.setParticularContent(informationEntity.getParticularContent());
        information.setStatus(0);

        int upbyid = informationMapper.updateById(information);
        HashMap<String, Object> map = new HashMap<>();
        map.put("upbyid",upbyid);
        return ResponseResult.ok(1,"成功",map);
    }

}
